package com.sqfw.project.wpReport.service.impl;


import com.github.pagehelper.PageInfo;
import com.sqfw.common.constant.HttpStatus;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.domain.WpVisitInfo;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.wp.mapper.WpVisitInfoMapper;
import com.sqfw.project.wpReport.domain.ScidTypeReport;
import com.sqfw.project.wpReport.domain.dto.WpInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wpReport.service.IScidTypeReportService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 坐席工作量汇总表Service业务层处理
 * 
 * @author lx
 * @date 2023-07-27
 */
@Slf4j
@Service
public class ScidTypeReportServiceImpl implements IScidTypeReportService 
{

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private WpInfoMapper wpInfoMapper;

    @Autowired
    private WpVisitInfoMapper wpVisitInfoMapper;

    //业务组
    private static final Long deptId = 101L;

    @Override
    public TableDataInfo getScidTypeReportList(ScidTypeReport scidTypeReport) {

        List<ScidTypeReport> scidTypeReports = new ArrayList<>();
        SysUser sysUser = new SysUser();
        sysUser.setCode(scidTypeReport.getCode());
        sysUser.setNickName(scidTypeReport.getNickName());
        sysUser.setDeptId(deptId);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(new PageInfo(sysUsers).getTotal());


        //未查询到对应坐席人员,直接返回
        if (sysUsers.size() == 0) {
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("查询成功");
            tableDataInfo.setRows(scidTypeReports);
            return tableDataInfo;
        }

        WpInfo wpInfo = new WpInfo();
        wpInfo.setBegainTime(scidTypeReport.getBegainTime());
        wpInfo.setEndTime(scidTypeReport.getEndTime());

        WpVisitInfo visitInfo = new WpVisitInfo();
        visitInfo.setBegainTime(scidTypeReport.getBegainTime());
        visitInfo.setEndTime(scidTypeReport.getEndTime());
        for (SysUser user : sysUsers) {
            wpInfo.setSender(user.getCode());

            ScidTypeReport scidTypeReportCon = new ScidTypeReport();
            scidTypeReportCon.setNickName(user.getNickName());
            scidTypeReportCon.setCode(user.getCode());
            long a = System.currentTimeMillis();
            List<WpInfoDTO> dtos = wpInfoMapper.countWpInfoGroupByScidType(wpInfo);
            log.info("countWpInfoGroupByScidType : {}, 工号：{}", System.currentTimeMillis() - a, user.getCode());
            for (WpInfoDTO dto : dtos) {
                if ("弹屏建单".equals(dto.getScidType())){
                    scidTypeReportCon.setScreenCount(dto.getCountNumber());
                }
                if ("手工建单".equals(dto.getScidType())){
                    scidTypeReportCon.setManualCount(dto.getCountNumber());
                }
                if ("12345工单".equals(dto.getScidType())){
                    scidTypeReportCon.setWpCount(dto.getCountNumber());
                }
                if ("110联动".equals(dto.getScidType())){
                    scidTypeReportCon.setAlarmCount(dto.getCountNumber());
                }
                if ("一网通办".equals(dto.getScidType())){
                    scidTypeReportCon.setNetworkCount(dto.getCountNumber());
                }
                if ("在线帮办".equals(dto.getScidType())){
                    scidTypeReportCon.setOnlineCount(dto.getCountNumber());
                }
                if ("专项整治".equals(dto.getScidType())){
                    scidTypeReportCon.setSpecialCount(dto.getCountNumber());
                }
                if ("白事热线".equals(dto.getScidType())){
                    scidTypeReportCon.setFuneralCount(dto.getCountNumber());
                }
                if ("12345信息单".equals(dto.getScidType())){
                    scidTypeReportCon.setInformationCount(dto.getCountNumber());
                }

            }

            if (scidTypeReportCon.getScreenCount()==null){
                scidTypeReportCon.setScreenCount(0);
            }
            if (scidTypeReportCon.getManualCount()==null){
                scidTypeReportCon.setManualCount(0);
            }
            if (scidTypeReportCon.getWpCount()==null){
                scidTypeReportCon.setWpCount(0);
            }
            if (scidTypeReportCon.getAlarmCount()==null){
                scidTypeReportCon.setAlarmCount(0);
            }
            if (scidTypeReportCon.getNetworkCount()==null){
                scidTypeReportCon.setNetworkCount(0);
            }
            if (scidTypeReportCon.getOnlineCount()==null){
                scidTypeReportCon.setOnlineCount(0);
            }
            if (scidTypeReportCon.getSpecialCount() == null) {
                scidTypeReportCon.setSpecialCount(0);
            }
            if (scidTypeReportCon.getFuneralCount() == null) {
                scidTypeReportCon.setFuneralCount(0);
            }
            if (scidTypeReportCon.getInformationCount() == null) {
                scidTypeReportCon.setInformationCount(0);
            }

          /*  //自办
            wpInfo.setEndState("自办");
            List<WpInfo> selfCount = wpInfoMapper.selectWpInfoList(wpInfo);

            scidTypeReportCon.setSelfCount(selfCount.size());*/

            //回访量
            wpInfo.setEndState(null);
           // wpInfo.setVisitState("1");
            visitInfo.setVisitWorkNo(user.getCode());
            Integer visitCount = wpVisitInfoMapper.countWpVisitInfoGroupByVisitWorkNo2(visitInfo);
            scidTypeReportCon.setVisitCount(visitCount);

            //有来源的集合
//            int count = dtos.stream().map(WpInfoDTO::getCountNumber).reduce(Integer::sum)
//                    .orElse(0);
            int count = scidTypeReportCon.getScreenCount() + scidTypeReportCon.getManualCount() +
                    scidTypeReportCon.getWpCount() + scidTypeReportCon.getAlarmCount() +
                    scidTypeReportCon.getNetworkCount() + scidTypeReportCon.getOnlineCount() +
                    scidTypeReportCon.getSpecialCount() + scidTypeReportCon.getFuneralCount() +
                    scidTypeReportCon.getInformationCount();
            // 再加上
            scidTypeReportCon.setTotalCount(count +visitCount);
            scidTypeReports.add(scidTypeReportCon);


        }

        tableDataInfo.setRows(scidTypeReports);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }

    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
}
